﻿using FreeSwitch.EventSocket.Parameters;
using FreeSwitch.EventSocket.Parser;

namespace FreeSwitch.EventSocket.Commands.CallManagement
{
	/// <summary>
	/// Make a new call from someone
	/// </summary>
	public class Originate : ICommand
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="Originate"/> class.
		/// </summary>
		public Originate(string callerUri, string destination)
		{
			CallerUri = callerUri;
			Destination = destination;
			CallerParameters = new ParameterCollection();
		}

		/// <summary>
		/// Gets or sets destination being called.
		/// </summar>
		/// <remarks>
		/// Dial plan string or sip uri.
		/// </remarks>
		public string Destination { get; set; }

		/// <summary>
		/// Gets or sets freeswitch uri of caller.
		/// </summary>
		/// <remarks>
		/// Freeswitch URIs consists of transport, profile name and username, each part is separated by a slash.
		/// </remarks>
		/// <example>
		/// var originate = new Originate("sofia/internal/1233", "1234");
		/// </example>
		public string CallerUri { get; set; }

		/// <summary>
		/// Gets collection of parameters that's used to control the dialing.
		/// </summary>
		public ParameterCollection CallerParameters { get; private set; }

		/// <summary>
		/// Gets or sets which context to use.
		/// </summary>
		/// <remarks>
		/// Per default there are a profile called "internal" and one called "external" in freeswitch.
		/// </remarks>
		public string DialplanContext { get; set; }

		/// <summary>
		/// Gets or sets which type of dial plan to use.
		/// </summary>
		/// <remarks>
		/// directory (ldap) and xml are built in types.
		/// </remarks>
		public string DialplanType { get; set; }

		/// <summary>
		/// Gets or sets caller's phone number.
		/// </summary>
		public string CallerIdNumber { get; set; }

		/// <summary>
		/// Gets or sets caller's name.
		/// </summary>
		public string CallerIdName { get; set; }

		/// <summary>
		/// Gets or sets originate timeout.
		/// </summary>
		/// <remarks>0 = default time</remarks>
		public int Timeout { get; set; }

		#region ICommand Members

		/// <summary>
		/// Parses a reply sent from FreeSWITCH for a command.
		/// </summary>
		/// <param name="msg"></param>
		/// <returns></returns>
		public ICommandReply ParseReply(ParsedMessage msg)
		{
			return CommandReply.Parse(msg);
		}

		/// <summary>
		/// Generate a string that can be sent to FreeSWITCH
		/// </summary>
		/// <returns>String that FreeSWITCH can interpret as a command.</returns>
		public string ToCommandString()
		{
			string tmp = string.Format("api originate {0} {1}", CallerUri, Destination);

			if (string.IsNullOrEmpty(DialplanType))
				return tmp;
			tmp += string.Format(" {0}", DialplanType);

			if (string.IsNullOrEmpty(DialplanContext))
				return tmp;
			tmp += string.Format(" {0}", DialplanContext);

			if (string.IsNullOrEmpty(CallerIdNumber))
				return tmp;
			tmp += string.Format(" {0}", CallerIdNumber);

			if (string.IsNullOrEmpty(CallerIdName))
				return tmp;
			tmp += CallerIdName;

			if (Timeout != 0)
				tmp += string.Format(" {0}", Timeout);

			return tmp;
		}

		#endregion
	}
}